package com.kye.express.business.welfare;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.integral.request.RequestUserIntegral;
import com.kye.model.bean.integral.request.ResultGetIntegralModel;
import com.kye.model.bean.welfare.RequestWelfareLiPinDH;
import com.kye.model.bean.welfare.ResponseWelfareLiPinDH;
import com.kye.model.datasource.WelfareDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 礼物界面 Presenter
 * <p>
 * Created by SkyYu on 2017/12/18
 */
public interface WelfareContract {

    interface WelfareView extends IView {
        void onLoadUser(User user);

        void onLoadIntegral(ResultGetIntegralModel bean);

        void onLoadIntegralApiError(OpenApiResult<ResultGetIntegralModel> result, int code, String msg);

        void onLoadWelFareList(OpenApiResult<ResponseWelfareLiPinDH> result);

        void onLoadWelFareListApiError(OpenApiResult<ResponseWelfareLiPinDH> result, int code, String msg);

        void onOtherError(Throwable e);
    }

    class WelfarePresenter extends BasePresenter<WelfareView> {

        private WelfareDataSource dataSource;

        WelfarePresenter() {
            dataSource = new WelfareDataSource();
        }

        @Override
        public void bind(WelfareView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onLoadUser(user);
                        }
                    });
        }

        void requestToGetAvailableIntegral(String autograph, final String phone) {
            dataSource.getWelfareList((new RequestUserIntegral(autograph, phone)))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultGetIntegralModel>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultGetIntegralModel> result, ResultGetIntegralModel resultGetIntegralModels) {
                            getView().onLoadIntegral(resultGetIntegralModels);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultGetIntegralModel> result, int code, String msg) {
                            getView().onLoadIntegralApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(e);
                        }
                    });
        }

        void requestToGetWelfare() {
            dataSource.getWelfareList(new RequestWelfareLiPinDH())
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResponseWelfareLiPinDH>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResponseWelfareLiPinDH> result, ResponseWelfareLiPinDH resultBean) {
                            getView().onLoadWelFareList(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResponseWelfareLiPinDH> result, int code, String msg) {
                            getView().onLoadWelFareListApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherError(e);
                        }
                    });
        }
    }
}